<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Exception;

class User extends Controller
{
    public function checkUserLoginInfo()
    {
        $re = [];
        $re['errorMessage'] = null;
        $re['success'] = false;
        $re['data'] = null;
        if (session('?userinfo')) {
            $re['data'] = session('userinfo');
            $re['success'] = true;
        } else {
            $re['errorMessage'] = '用户尚未登录';
        }
        return json($re);
    }

    public function getDepartment()
    {
        $list = Db::table('department')->select();
        return json([
            'data' => $list,
            'success' => true
        ]);
    }

    public function getUserInfo()
    {
        $department = Db::table('department')->find(session('userinfo.department'))['name'];
        $username = session('userinfo.username');
        $telephone = session('userinfo.telephone');
        return json([
            'success' => true,
            'data' => compact('department', 'username', 'telephone')
        ]);
    }

    public function userLogin($username = null, $password = null)
    {
        //此处的username实际为telephone，原作的参数写错了
        if (!$username || !$password) {
            return json([
                'errorMessage' => '缺少字段',
                'success' => false
            ]);
        }
        $userinfo = Db::table('user')->where('telephone', $username)->where('password', md5($password))->find();
        if (!$userinfo) {
            return json([
                'errorMessage' => '手机号或密码错误',
                'success' => false
            ]);
        } else {
            session('userinfo', $userinfo);
            return json(['success' => true]);
        }
    }

    public function userLogout()
    {
        try {
            session('userinfo', null);
            return json([
                'data' => '注销登录成功！',
                'success' => true
            ]);
        } catch (Exception $e) {
            return json([
                'data' => $e->getMessage(),
                'success' => false
            ]);
        }
    }

    public function addUser($username = null, $telephone = null, $password = null, $department = null)
    {
        if (!$username || !$telephone || !$password || !$department) {
            return json([
                'errorMessage' => '注册必要信息不全！',
                'success' => false
            ]);
        }
        $other = Db::table('user')->where('username', $username)->find();
        if ($other) {
            return json([
                'errorMessage' => '此用户名已存在！',
                'success' => false
            ]);
        }
        $other = Db::table('user')->where('telephone', $telephone)->find();
        if ($other) {
            return json([
                'errorMessage' => '此手机号已存在！',
                'success' => false
            ]);
        }
        if (!preg_match("/^\d{11}$/", $telephone)) {
            return json([
                'errorMessage' => '手机号格式不正确！',
                'success' => false
            ]);
        }
        $re = Db::table('user')->insert([
            'username' => $username,
            'telephone' => $telephone,
            'password' => md5($password),
            'department' => $department
        ]);
        if (!$re) {
            return json([
                'errorMessage' => '数据库插入错误，请刷新重试！',
                'success' => false
            ]);
        }
        return json([
            'errorMessage' => '注册成功！',
            'success' => true
        ]);
    }
}
